/*
 * cache.c
 *
 *  Created on: Jun 2, 2014
 *      Author: mitchellalpert
 */

#include "cache.h"
#include <math.h>
#include <stdlib.h>

static cache_config config;
static cache_row *cache;

void initialize_cache(cache_config cc) {
	config = cc;
	int i;

	int rows = config.cache_size / config.block_size / config.associativity;
	cache = malloc(rows * sizeof(cache_row));

	for(i = 0; i < rows; i++) {
		cache[i].valid = 0;
		cache[i].tag = 0;
		cache[i].LRS = 0;
	}

}

cache_result access_cache(unsigned int byte_address) {
	cache_result result;

	int rows = config.cache_size / config.block_size / config.associativity;
	int index_size = log(rows);
	int index = (byte_address % rows);
	int tag = (byte_address - index) / rows;

	result.block_address = (tag << index_size) + index;
	result.index = index;
	result.tag = tag;

	if (cache[index].valid == 0 || cache[index].valid == 0 || cache[index].tag != tag) {
		result.hit = 0;
	}


	return result;
}
